var Global = cc.Class({
    extends: cc.Component,

    properties: {
        ip: "",
    },
 
    ctor: function () {
        cc.log("Net ctor");
        this.socket=null;
        this.handlers={};
        this.status = 0;
    },

    addHandler:function(event,fn){
        if(this.handlers[event]){
            cc.log("event:" + event + "' handler has been registered.");
            return;
        }

        var handler = function(data){
            //cc.log(event + "(" + typeof(data) + "):" + (data? data.toString():"null"));
            if(event != "disconnect" && typeof(data) == "string"){
                data = JSON.parse(data);
            }
            fn(data);
        };
        
        this.handlers[event] = handler; 
        if(this.socket){
            cc.log("register:function " + event);
            this.socket.on(event, handler);
        }
    },
    
    removeHandler:function(){
        this.handler[event] = nil;
    },

    removeAllHandler:function(){
        this.handlers = {};
        if (this.socket) {
            this.socket.off();
        }
    },
    // query connector
    queryEntry:function (uid, callback) {
        var pomelo = window.pomelo;
        var route = 'gate.gateHandler.queryEntry';
        pomelo.init({
            host: "120.79.61.215",
            port: 3014,
            log: true
        }, function () {
            pomelo.request(route, {
                uid: uid
            }, function (data) {
                pomelo.disconnect();
                if (data.code === 500) {
                    showError(LOGIN_ERROR);
                    return;
                }
                callback(data.host, data.port);
            });
        });
    },

    connect:function() {
        // if (this.ip === "") {
        //     cc.log("ip is empty.");
        //     return;
        // }
        // var self = this;
        // var opts = {
        //     'reconnection':false,
        //     'force new connection': true,
        //     'transports':['websocket', 'polling']
        // }
        // cc.log("ip is %s", this.ip);
        // this.socket = window.io.connect(this.ip, opts);
        // this.socket.on('reconnect',function(){
        //     cc.log('reconnection');
        // });
        // this.socket.on('connect',function(data){
        //     cc.log("connect");
        //     self.socket.connected = true;
        // });
        // this.socket.on('disconnect',function(data){
        //     cc.log("disconnect");
        //     self.socket.connected = false;
        //     self.close();
        // });
        // this.socket.on('connect_failed',function (){
        //     cc.log('connect_failed');
        // });
        // this.socket.on('connect_error',function (){
        //     cc.log('connect_error');
        // });
        // for(var key in this.handlers){
        //     var value = this.handlers[key];
        //     if(typeof(value) == "function"){
        //         cc.log("register:function " + key);
        //         this.socket.on(key,value);
        //     }
        // }
        var pomelo = window.pomelo;
        this.queryEntry(1, function(host, port) {
			pomelo.init({
				host: host,
				port: port,
				log: true
			}, function() {
				var route = "connector.entryHandler.enter";
				pomelo.request(route, {
					username: "Jacky",
					rid: 1
				}, function(data) {
					if(data.error) {
                        cc.log(data.error);
						return;
					}
				});
			});
		});
    },

    send:function(event,data){
        if(this.socket&&this.socket.connected){
            if(data != null && (typeof(data) == "object")){
                data = JSON.stringify(data);
                //cc.log(data);              
            }
            this.socket.emit(event,data);
        }
    },

    close:function(){
    	cc.log('close');
        this.delayMS = null;
        if(this.socket && this.socket.connected){
            this.socket.connected = false;
            this.socket.disconnect();
        }
        this.socket = null;
    },
});